package com.homedo.odin.solution.controller.mng;

import com.homedo.core.common.base.controller.BaseController;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.odin.solution.model.request.mng.MngEquipmentReqsListRequestBean;
import com.homedo.odin.solution.model.request.mng.MngEquipmentUpdateRequestBean;
import com.homedo.odin.solution.model.response.mng.MngEquipmentResponseBean;
import com.homedo.odin.solution.service.EquipmentListServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 设备清单报备接口
 *
 * @author chenlei
 * @date 2018/5/22
 */
@Api(description = "设备清单报备接口", tags = {"Equipment"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/mng/equipment")
public class MngEquipmentController extends BaseController<EquipmentListServiceImpl> {

    @ApiOperation(value = "Mng_设备清单报备列表查询", httpMethod = "GET", response = MngEquipmentResponseBean.class)
    @GetMapping()
    public ResultData getEquipments(@Valid @ModelAttribute MngEquipmentReqsListRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultData
                    .newInstance(new Meta(Meta.CODE_451.getCode(), bindingResult.getFieldError().getDefaultMessage()));
        }

        return ResultData.newInstance(baseService.getEquipments(requestBean));
    }

    @ApiOperation(value = "Mng_设备清单修改", httpMethod = "POST")
    @PostMapping("/{equipmentId}")
    public ResultData updateEquipment(@PathVariable("equipmentId") Long equipmentId,
            @Valid @RequestBody MngEquipmentUpdateRequestBean requestBean, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultData
                    .newInstance(new Meta(Meta.CODE_451.getCode(), bindingResult.getFieldError().getDefaultMessage()));
        }
        if (equipmentId < 0) {
            throw new BizException(Meta.CODE_451.getCode(), "equipmentId参数有误");
        }

        baseService.updateTrackRecord(equipmentId, requestBean);

        return ResultData.newInstance();
    }
}
